package com.javarush.test.level20.lesson02.task01;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/* Читаем и пишем в файл: Human
Реализуйте логику записи в файл и чтения из файла для класса Human
Поле name в классе Human не может быть пустым
В файле your_file_name.tmp может быть несколько объектов Human
Метод main реализован только для вас и не участвует в тестировании
*/
public class Solution {
    public static void main(String[] args) {
        //you can find your_file_name.tmp in your TMP directory or fix outputStream/inputStream according to your real file location
        //вы можете найти your_file_name.tmp в папке TMP или исправьте outputStream/inputStream в соответствии с путем к вашему реальному файлу
        try {

            File temp = File.createTempFile("D:\\temp.tmp", null);
            OutputStream outputStream = new FileOutputStream("D:\\temp.tmp");
            InputStream inputStream = new FileInputStream("D:\\temp.tmp");

            Human ivanov = new Human("Ivanov");
            Human ivanov2 = new Human("Ivanov2", new Asset("home2"), new Asset("car2"));
            ivanov.save(outputStream);
            ivanov2.save(outputStream);
            outputStream.flush();
            outputStream.close();

            Human somePerson = new Human();
            Human somePerson2=new Human();
            somePerson.load(inputStream);
            //somePerson2.load(inputStream);
            System.out.println(ivanov.equals(somePerson));

            inputStream.close();

        } catch (IOException e) {
            //e.printStackTrace();
            System.out.println("Oops, something wrong with my file");
        } catch (Exception e) {
            //e.printStackTrace();
            System.out.println("Oops, something wrong with save/load method");
        }
    }


    public static class Human {
        public String name;
        public List<Asset> assets = new ArrayList<>();

        public Human() {
        }

        public Human(String name, Asset... assets) {
            this.name = name;
            if (assets != null) {
                this.assets.addAll(Arrays.asList(assets));
            }
        }

        public void save(OutputStream outputStream) throws Exception {
            PrintWriter printWriter = new PrintWriter(outputStream);
            if (name!=null){
                printWriter.println(name);
                if (this.assets != null) {
                    for (Asset as : assets) {
                        printWriter.println("Yes");
                        printWriter.println(as.getName());
                        if (as.getPrice() != 0.0)
                            printWriter.println(as.getPrice());
                        else
                            printWriter.println("n");
                    }
                }
            }
            printWriter.println("end");
            printWriter.flush();
        }

        public void load(InputStream inputStream) throws Exception {
            BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
            int count=0;
            name =reader.readLine();

            String isAssets;
            while ((isAssets=reader.readLine()).equals("Yes"))
            {
                assets.add(new Asset(reader.readLine()));
                String price=reader.readLine();
                if(!price.equals("n"))
                        assets.get(count).setPrice(Double.parseDouble(price));
                count++;
            }

        }
    }
}
